package org.example.springioc.controller;

import org.example.springioc.component.HelloComponent;
import org.example.springioc.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/21
 * Time:17:31
 */
@Controller
public class HelloController {

    //1.属性注入
    @Autowired
    private HelloService helloService;
    @Autowired
    private HelloComponent helloComponent;

    //2.构造方法注入
    //如果只有一个构造方法,只能使用该构造方法
    //当存在多个构造方法时,如果有无参构造方法,默认调用无参构造方法
    //当存在多个构造方法时,如果没有无参构造方法,Spring就不知道该用哪个构造方法,需要使用@Autowired指定一个默认的构造方法
    //构造方法使用规范
    //(1)当添加有参构造方法时,显式添加无参构造方法
    //(2)如果存在多个构造方法,使用@Autowired指定一个默认的构造方法
    public HelloController(){
    }
    public HelloController(HelloService helloService) {
        this.helloService = helloService;
    }
    @Autowired
    public HelloController(HelloService helloService, HelloComponent helloComponent) {
        this.helloService = helloService;
        this.helloComponent = helloComponent;
    }

    //3.Setter方法注入
    //搭配@Autowired使用
    @Autowired
    public void setHelloService(HelloService helloService) {
        this.helloService = helloService;
    }
    @Autowired
    public void setHelloComponent(HelloComponent helloComponent) {
        this.helloComponent = helloComponent;
    }

    public void demo() {
        helloService.demo();
        helloComponent.demo();
        System.out.println("Hello HelloController");
    }
}
